# user_list = ["admin","morning","eight","Mary"]
# pwd_list = ["1234","2234","3234","4234"]
import time
user_list = [{'username':'admin','password':'1234','phone':'15812345678','balance':2349.7,'history':''},
             {'username':'eight','password':'2234','phone':'13526354819','balance':635.9,'history':''}]
login_index = -1
# 判断一个字符串能否被转换为一个有效的数字
def check_number(string):
    point=0
    isvalid=True
    for c in string:
        code=ord(c)
        if (code < 48 and code != 46) or code > 57:
            isvalid = False
            break
        elif code==46:point+=1
    if point>1:isvalid = False
    return isvalid
# 检查一个用户名是否存在，存在返回下标，不存在返回-1
def check(name):
    for i in range(0, len(user_list)):
        if user_list[i]['username'] == name:
            return i
    return -1
# 注册
def reg():
    name = input("[注册]请输入用户名：")
    pwd = input("[注册]请输入密码：")
    phone = input("[注册]请输入手机号码：")
    if check(name) >= 0:
        print("用户名已存在！")
    else:
        print("注册成功！")
        user_info = {}
        user_info['username'] = name
        user_info['password'] = pwd
        user_info['phone'] = phone
        user_info['balance'] = 0
        user_info['history'] = ''
        user_list.append(user_info)
        print(user_list)
        start_menu()
# 登录
def login():
    name = input("[登录]请输入用户名：")
    pwd = input("[登录]请输入密码：")
    index = check(name)
    if index >= 0:
        if user_list[index]['password'] == pwd:
            print("登录成功")
            global login_index
            login_index = index
            main_menu()
        else:
            print("密码错误")
    else:
        print("用户名错误")
def query():
    balance = user_list[login_index]['balance']
    return balance
def withdraw():
    c = input("请输入你的取款金额：")
    if check_number(c):
        c = float(c)
        if c <= query():
            user_list[login_index]['balance'] -= c
            s = query()
            print(f"取款成功！当前余额为：{s}")
            t = time.asctime(time.localtime(time.time()))
            present = '取款:'+str(c)
            history_str = user_list[login_index]['history']
            user_list[login_index]['history'] =   history_str + '!' + t + '!' + present + '!'
            # print(user_list[login_index]['history'])   #取款后当前用户的流水记录
        else:
            print("余额不足")
    else:
        withdraw()
    main_menu()
def deposite():
    c = input("请输入你的存款金额：")
    if check_number(c):
        c = float(c)
        user_list[login_index]['balance'] += c
        s = query()
        print(f"存款成功！当前余额为：{s}")
        t = time.asctime(time.localtime(time.time()))
        present = '存款:' + str(c)
        history_str = user_list[login_index]['history']
        user_list[login_index]['history'] = history_str + '!' + t + '!' + present + '!'
        # print(user_list[login_index]['history'])   # 存款后当前用户的流水记录
    main_menu()
def transer():
    account = input("请输入你的转账账号：")
    d = check(account)
    if d >= 0 and d != login_index:
        c = input("请输入你的转账金额：")
        if check_number(c):
            c = float(c)
            if c <= query():
                user_list[login_index]['balance'] -= c
                user_list[d]['balance'] += c
                s = query()
                print(f"转账成功！当前余额为：{s}")
                print(f"另一个账户余额为：{user_list[d]['balance']}")  # 方便检查另一个账户是否有进账
                t = time.asctime(time.localtime(time.time()))
                present = '账户' + user_list[login_index]['username'] + '转入:' + str(c)
                history_str = user_list[d]['history']
                user_list[d]['history'] = history_str + '!' + t + '!' + present + '!'
                # print(user_list[d]['history'])       # 被转账用户的流水记录
                present1 = '转出' + str(c) + '到账户' + user_list[d]['username']
                history_str = user_list[login_index]['history']
                user_list[login_index]['history'] = history_str + '!' + t + '!' + present1 + '!'  # print(user_list[login_index]['history'])   # 转账后当前用户的流水记录
            else:
                print("余额不足")
                transer()
        else:
            print("你输入的金额不符合规范")
            transer()
    elif d == login_index:
        print("你输入的账号为当前账号")
        transer()
    else:
        print("你输入的账号不存在")
        transer()
    main_menu()
def history():
    string = user_list[login_index]['history']
    print(string.split('!'))
    main_menu()
def update_pwd():
    j = True
    while j:
        new_pwd1 = input("请输入新密码：")
        new_pwd2 = input("请重复输入新密码：")
        if new_pwd1 == new_pwd2:
            break
    user_list[login_index]['password'] = new_pwd1
    print(user_list[login_index])
    main_menu()
# 开始菜单
def start_menu():
    tip = '''
********************* 欢迎来到蜗牛ATM *********************
********************** 请选择以下操作 *********************
****************** 1.注册  2.登录  3.退卡 *****************         
    '''
    print(tip)
    option = int(input("请输入你的选择："))
    if option == 1:
        reg()
    elif option == 2:
        login()
    elif option == 3:
        exit("退卡成功")

def main_menu():
    tip = '''
***************************** 进入蜗牛ATM主菜单 *****************************
****************************** 请选择以下操作 *******************************
******************* 1. 查询   2. 取款   3. 存款   4. 转账 *******************
****************** 5. 流水  6. 修改密码   7. 返回   8. 退卡 *****************     
        '''
    print(tip)
    option = int(input("请输入你的选择："))
    if option == 1:
        s = query()
        print(f"当前余额为：{s}" )
        main_menu()
    elif option == 2:
        withdraw()
    elif option == 3:
        deposite()
    elif option == 4:
        transer()
    elif option == 5:
        history()
    elif option == 6:
        update_pwd()
    elif option == 7:
        start_menu()
    elif option == 8:
        exit('谢谢，欢迎下次光临.')



start_menu()

